#include <stdlib.h>
#include <iostream>
using namespace std;

// https://www.cnblogs.com/chuanfengzhang/p/8447251.html#!comments
// 注意写覆盖
// 批量

void* i_memcpy(void* dst, const void* src, int n){
    if(dst == NULL || src == NULL || n <= 0) return NULL;

    int* pdst = (int*) dst;
    int* psrc = (int*) src;
    char* tmp1 = NULL;
    char* tmp2 = NULL;
    int c1 = n / 4;
    int c2 = n % 4;

    if(pdst > psrc && (char*)pdst < (char*)psrc + n){
        tmp1 = (char*) pdst + n - 1;
        tmp2 = (char*) psrc + n - 1;
        while(c2--) *tmp1-- = *tmp2--;
        
        tmp1++; tmp2++;
        pdst = (int*) tmp1;
        psrc = (int*) tmp2;
        pdst--; psrc--;
        while(c1--)
            *pdst-- = *psrc--;
    }
    else{
        while(c1--)
            *pdst++ = *psrc++;
        tmp1 = (char*) pdst;
        tmp2 = (char*) psrc;
        while(c2--) *tmp1++ = *tmp2++;
    }
    return dst;
}

void show(int arr[], int size){
    for(int i = 0; i < size; ++i)
        cout << arr[i] << " ";
    cout << endl;
}

int main(int argc, char const *argv[])
{
    
    int arr[] = {1,2,3,4,5,6,7};
    int* arr2 = new int[10];
    show(arr2, 10);

    i_memcpy(arr2, arr, 7 * sizeof(int));
    show(arr2, 10);

    i_memcpy(arr, arr + 3, 3 * sizeof(int));
    show(arr, 7);
    
    return 0;
}
